/*****************************************************************************

File:   otkModelUtils.java

Date          Build     Author        Version    Comments
12-Oct-12    P-20-14    dshah          $$1         Created
20-mar-15 P-30-05 gshmelev $$2  called GetCurrentSession

****************************************************************************/

package com.ptc.otkjavaexamples;
 
import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcSession.*;
import com.ptc.wfc.wfcModel.*;
import com.ptc.wfc.wfcAssembly.*;

/* 
 
 Utilites for wfcWModel
  
*/
class otkModelUtils extends DefaultUICommandActionListener
{



    public otkModelUtils()
	{
	}
	
	
	
	
	public void OnCommand()
	{
		DumpModelInfo();
	}
	
	public int DumpModelInfo()
	{

		try
		{
			FileWriter file1 = new FileWriter("DumpModelInfo.inf");
			BufferedWriter InfoFile = new BufferedWriter(file1);
			
			FileWriter file2 = new FileWriter("DumpModelInfo.log");
			BufferedWriter LogFile = new BufferedWriter(file2);

			Session session = pfcSession.GetCurrentSession ();
			WSession wSession = (WSession)(session);

			Model wCurrentModel = wSession.GetCurrentModel();

			WModel wModel = (WModel)(wCurrentModel);

			if ( wCurrentModel == null )
			{
			InfoFile.write("\t - There is no Current Model. ");
			InfoFile.newLine();
			}
			else
			{
			int status =  OtkUtilWriteModelInfo ( wSession,
												  wModel,
												  InfoFile,
											      LogFile,
												  3 );
			LogFile.write("status " + status
                   + " returned from OtkUtilWriteModelInfo ()");
			LogFile.newLine();
			}
			InfoFile.close();
			LogFile.close();
			
			Double XOffset = new Double(0);
			Double YOffset = new Double(0);
			Integer rows = new Integer(0);
			Integer columns = new Integer(0);

			wSession.DisplayInformationWindow("DumpModelInfo.inf", XOffset, YOffset, rows, columns);
		}
		catch (Exception e)
		{
			System.out.println("1: " + e);
		}	
		return 0;
	}
	
	//==============================================================================
	int OtkUtilWriteAssemblyInfo (
       WSession        wSession,   // input - Session Pointer
       WModel          wModel,     // input - Model Pointer
       BufferedWriter  InfoFile,   // input - InfoFile
       BufferedWriter  LogFile,    // input - LogFile
       int             level )     // input - level details of info to be written
	//------------------------------------------------------------------------------
	// Purpose: Writes Assembly Information
	//------------------------------------------------------------------------------
	{
		int status = 0;

		if( wSession == null)
			return -1;

		if( wModel == null)
			return -2;

		try
		{
			ModelType wModelType = wModel.GetType();

			if ( wModelType != ModelType.MDL_ASSEMBLY )
				return -3;

			WAssembly wAssembly = (WAssembly)(wModel);

			InfoFile.write("====================================================");
			InfoFile.newLine();
			InfoFile.write("|                  Assembly Info                   |");
			InfoFile.newLine();
			InfoFile.write("====================================================");
			InfoFile.newLine();

			boolean wIsExploded = wAssembly.GetIsExploded ();
			if ( true == wIsExploded )
			{
				InfoFile.write("\t - This is an Exploded Assembly.");
				InfoFile.newLine();
			}

			boolean wDynamicPositioning = wAssembly.GetDynamicPositioning ();

			if ( true == wDynamicPositioning )
			{
				InfoFile.write("\t - Assembly uses dynamic positioning.");
				InfoFile.newLine();
			}

			Connectors wConnectors = wAssembly.GetConnectors ();

			if ( null != wConnectors )
			{
				int size = wConnectors.getarraysize ();
    
				InfoFile.write("\t - There are " + size + " Connectors in the assembly." );
				InfoFile.newLine();
			}

			Harnesses wHarnesses = wAssembly.GetHarnesses ();

			if ( null != wHarnesses )
			{
				int size = wHarnesses.getarraysize ();
    
				InfoFile.write("\t - There are " + size + " Harnesses in the assembly." );
				InfoFile.newLine();
			}

			LineStocks wLinestocks = wAssembly.GetLinestocks ();

			if ( null != wLinestocks )
			{
				int size = wLinestocks.getarraysize ();
    
				InfoFile.write("\t - There are " + size + " Linestocks in the assembly." );
				InfoFile.newLine();
			}

			Spools wSpools = wAssembly.GetSpools ();

			if ( null != wSpools )
			{
				int wSpoolsSize = wSpools.getarraysize ();
    
				InfoFile.write("\t - There are " + wSpoolsSize + " Spools in the assembly." );
				InfoFile.newLine();

				for ( int Idx = 0; Idx < wSpoolsSize ; Idx++ )
				{
					String wSpoolName = (wSpools.get(Idx)).GetName ();
					InfoFile.write("\t - Spool [" + Idx + "] = "  + wSpoolName);
					InfoFile.newLine();
				}
			}

			WComponentPaths wDispComps = wAssembly.ListDisplayedComponents ();

			if ( null != wDispComps )
			{
				int wDispCompsSize = wDispComps.getarraysize ();
    
				InfoFile.write("\t - There are " + wDispCompsSize + " Displayed Components in the assembly.");
			    InfoFile.newLine();

				for ( int Idx = 0; Idx < wDispCompsSize ; Idx++ )
				{
					String wFileName = ( (wDispComps.get(Idx)).GetLeaf() ).GetFileName ();
					InfoFile.write("\t - Displayed Component [" + Idx + "] = " + wFileName);
					InfoFile.newLine();
				}
			}

			return status;
		}
		catch (Exception e)
		{
			System.out.println("1: " + e);
		}
		return 0;
	}

	//==============================================================================
	int OtkUtilWriteModelInfo (
       WSession             wSession, 	// input - Session Pointer
       WModel               wModel,     // input - Model Pointer
       BufferedWriter       InfoFile,   // input - InfoFile
       BufferedWriter       LogFile,    // input - LogFile
       int                  level )     // input - level details of info to be written
	//------------------------------------------------------------------------------
	// Purpose: Writes Model Information
	//------------------------------------------------------------------------------
	{
		int status = 0;

		if( wSession == null)
			return -1;

		if( wModel == null)
			return -2;

		try
		{
			InfoFile.write( "====================================================" );
			InfoFile.newLine();
			InfoFile.write( "|                    Model Info                    |" );
			InfoFile.newLine();
			InfoFile.write( "====================================================" );
			InfoFile.newLine();

			String wFileName = wModel.GetFileName();
			InfoFile.write( "\t - Model File Name : " + wFileName );
			InfoFile.newLine();

			ModelType wModelType = wModel.GetType();
			InfoFile.write( "\t - Model Type : ");

			switch  (wModelType.getValue() )
			{
				case ModelType._MDL_ASSEMBLY :
					InfoFile.write( "Assembly" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_PART :
					InfoFile.write( "Part" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_DRAWING :
					InfoFile.write( "Drawing" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_2D_SECTION :
					InfoFile.write( "2D Section" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_LAYOUT :
					InfoFile.write( "Notebook" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_DWG_FORMAT :
					InfoFile.write( "Drawing Format" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_MFG :
					InfoFile.write( "Manufacturing" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_REPORT :
					InfoFile.write( "Report" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_MARKUP :
					InfoFile.write( "Markup" );
					InfoFile.newLine();
					break;

				case ModelType._MDL_DIAGRAM :
					InfoFile.write( "Diagram" );
					InfoFile.newLine();
					break;

				case 10 :
					InfoFile.write( "Nil" );
					InfoFile.newLine();
					break;

				default:
					InfoFile.write( "Unknown" );
					InfoFile.newLine();
					break;
			}

			String wFullName = wModel.GetFullName();
			InfoFile.write( "\t - Model Full Name : " + wFullName );
			InfoFile.newLine();

			String wCommonName = wModel.GetCommonName ();
			if ( wCommonName != null )
			{
				InfoFile.write( "\t - Common Name : " + wCommonName );
				InfoFile.newLine();
			}

			String wGenericName = wModel.GetGenericName ();
			if ( wGenericName != null )
			{
				InfoFile.write( "\t - Generic Name : " + wGenericName );
				InfoFile.newLine();
			}

			String wInstanceName = wModel.GetInstanceName ();
			InfoFile.write( "\t - Instance Name : " + wInstanceName );
			InfoFile.newLine();

			String wVersion = wModel.GetVersion ();
			if ( wVersion != null )
			{
				InfoFile.write( "\t - Version : " + wVersion );
				InfoFile.newLine();
			}

			String wReleaseLevel = wModel.GetReleaseLevel ();
			if ( wReleaseLevel != null )
			{
				InfoFile.write( "\t - Release Level : " + wReleaseLevel );
				InfoFile.newLine();
			}

			String wRevision = wModel.GetRevision ();
			if ( wRevision != null)
			{
				InfoFile.write( "\t - Revision Number : " + wRevision );
				InfoFile.newLine();
			}

			String wVersionStamp = wModel.GetVersionStamp();
			InfoFile.write( "\t - Version Stamp : " + wVersionStamp );
			InfoFile.newLine();

			if ( wModelType == ModelType.MDL_ASSEMBLY )
			{
				status = OtkUtilWriteAssemblyInfo ( wSession, wModel, InfoFile,
                                           LogFile, level);
				LogFile.write( "status " + status + " returned from OtkUtilWriteAssemblyInfo()" );
				InfoFile.newLine();
			}

			return status;
		}
		catch (Exception e)
		{
			System.out.println("1: " + e);
		}
		return status;

	}
	

};






